<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query.queryMap"
      :query-more-json="true"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
    </page-filter>
    <dialog-table
      ref="resourceTable"
      v-model:data="tableInfo.data"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :row-key="rowKey"
      :checked-row-list="checkedRowList"
      :reserve-selection="true"
      :check-box="checkBox"
      :pager="pager"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-commonName="scope">
        {{ scope.row.commonName }}
      </template>
    </dialog-table>
  </page-dialog>
</template>
<script setup name="selectAsset">
import PageDialog from '@/components/PageDialog/index'
import DialogTable from '@/components/DialogTable/index'
import PageFilter from '@/components/NewPageFilter/index'
import { getCustomizeSetting } from '@/api/customize'
import { getEquipmentArchivesCustomizePage } from '@/api/equipment'
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  rowKey: {
    type: String,
    default: 'id',
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getEquipmentArchivesCustomizePage,
  },
  visible: {
    type: Boolean,
    default: false,
  },
  showPrice: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  queryParams: {
    type: Object,
    default: function () {
      return {}
    },
  },
  tableKey: {
    type: String,
    default:
      'assetStoreOrderModify-com.slip.spd.entity.vo.device.store.AssetStoreOrderDetailVO',
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  pager: {
    type: Boolean,
    default: true,
  },
})

const filterInfo = reactive({
  query: {
    customizeUserVO: {},
    queryMap: {},
    queryParams: {},
  },
  searchList: [],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
})
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
})
const dialogInfo = reactive({
  title: '选择资产',
  width: '90%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
let selectRow = reactive([])
const emits = defineEmits()

let shows = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件shows
  },
})
setData()
function setData() {
  let obj = {
    commonName: {
      label: '设备名称',
      value: 'commonName',
      componentType: 'input',
      show: false,
      type: 'slot',
      hidden: true,
    },
    statusValue: {
      label: '状态',
      value: 'statusValue',
      asValue: 'status',
      componentType: 'select-model',
      code: 'enabled_type',
      show: false,
      hidden: true,
    },
  }
  tableInfo.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}
function getList() {
  tableInfo.api(filterInfo.query).then((res) => {
    if (res && res.success) {
      tableInfo.data = res.data.mergeDetails || []
    }
  })
}
function search() {
  getList()
}
function save() {
  handleClose()
  emits('callback', selectRow)
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow = data
      if (!props.saveEmpty) {
        dialogInfo.btList[1].disabled = !selectRow
      }
      break
    case 'tableCheck':
      selectRow = data
      if (!props.saveEmpty) {
        dialogInfo.btList[1].disabled = !selectRow
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
function getCustomize() {
  let param = { tableKey: props.tableKey }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.fieldList = proxy.$fn.setFieldList(
        tableInfo.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.searchList,
        data.detail
      )
      filterInfo.searchList = searchInfo.searchList
      filterInfo.searchList.forEach((item) => {
        item.event = search
      })

      filterInfo.query.customizeUserVO = data
      filterInfo.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.query.queryMap,
        ...props.query,
      }
      filterInfo.query.queryParams = {
        ...filterInfo.query.queryParams,
        ...props.queryParams,
      }
      tableInfo.api = props.api
      getList()
    }
  })
}
</script>

<style scoped lang="scss"></style>
